package com.huarun.module.user.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.huarun.common.BaseServiceImpl;
import com.huarun.common.HeadAuthorization;
import com.huarun.common.ResponseMessage;
import com.huarun.common.util.*;
import com.huarun.module.sys.bean.Subject;
import com.huarun.module.sys.bean.UserProject;
import com.huarun.module.sys.dao.SubjectDao;
import com.huarun.module.sys.dao.UserProjectDao;
import com.huarun.module.sys.service.IUserProjectService;
import com.huarun.module.sys.util.UserInfoUtil;
import com.huarun.module.user.bean.User;
import com.huarun.module.user.dao.UserDao;
import com.huarun.module.user.service.IUserService;
import com.huarun.module.user.util.LesseeInfoUtil;
import com.huarun.module.webapi.IChunSunServiceApi;
import com.huarun.redis.RedisKeyPrefix;
import com.huarun.redis.RedisUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hqy
 * @创建时间 2018-10-12 11:43
 * @Description 类描述
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserDao, User> implements IUserService {

	@Resource
	private UserDao userDao;

	@Resource
	private UserProjectDao userProjectDao;
	@Resource
	private IChunSunServiceApi chunSunService;

	@Resource
	private IUserProjectService userProjecService;

	@Resource
	private SubjectDao subjcetDao;

	private WechatLogin wxLogin;

	public UserServiceImpl() {

		if (wxLogin == null) {
			wxLogin = new WechatLogin();
		}
	}

	@Override
	public User getUserByParams(Map<String, Object> params) {
		// 重写该出逻辑，用户ID和OPENID从数据库中取，其他信息从redis中取 by mengsg
		User user = userDao.getUserByParams(params);
		if (null == user || null == user.getId() || user.getId() < 0 || StringUtils.isEmpty(user.getOpenId())) {
			return null;
		}
		return getUserInfoFromRedis(user);
	}

	/**
	 * 通过请求微信登录，跳转后进行处理
	 * https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
	 * 授权成功返回： redirect_uri?code=CODE&state=STATE 授权失败返回：
	 * redirect_uri?state=STATE 获得code后获取access_token
	 * 
	 * @throws IOException
	 *
	 */
	@Override
	public ResponseMessage<HeadAuthorization> getSessionKey(String code, HttpServletRequest req) throws IOException {
		ResponseMessage<HeadAuthorization> rMessage = new ResponseMessage<HeadAuthorization>();
		String returnUrl = null;
		String sKey = null;
		if (code != null) {
			returnUrl = wxLogin.getSessionKey(code);
			if (returnUrl != null) {
				System.out.println(returnUrl);
				Map<String, Object> map = StringUtils.jasonConvertMap(returnUrl);
				if (map != null) {
					if (null != (String) map.get("errmsg")) {
						rMessage.setMessage((String) map.get("errmsg"));
						rMessage.setCode(ResultCodeEnum.ERROR.code);
					} else {
						String sessionKey = map.get("session_key").toString();
						String openId = map.get("openid").toString();
						JSONObject sKeyObj = new JSONObject();
						sKeyObj.put("sessionKey", sessionKey);
						sKeyObj.put("openid", openId);
						sKeyObj.put("date", System.currentTimeMillis());
						if (StringUtils.isBlank(sKey)) {
							rMessage.setMessage("生成token为空");
							rMessage.setCode(ResultCodeValue.R_TOKEN_IS_NULL);
						}
						String redisUrl = UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_TOKEN, openId, sessionKey);
						if (StringUtils.isBlank(redisUrl)) {
							rMessage.setMessage("保存token不成功");
							rMessage.setCode(ResultCodeValue.R_SAVE_TOKEN_ERROR);
						}
						rMessage.setMessage("成功");
						HeadAuthorization head = new HeadAuthorization();
						head.setOpenId(openId);
						head.setsKey(sKey);
						rMessage.setData(head);
						rMessage.setCode(ResultCodeEnum.SUCCESS.code);
					}
				}
			}
		}
		return rMessage;
	}

	@Override
	public ResponseMessage<String> decryptInfo(String encryptedData, String iv, HttpServletRequest req) {
		ResponseMessage<String> rMessage = new ResponseMessage<String>();
		String sessionKey = null;
		try {
			// 通过Head中的openId，得到SessionKey 用于后续的解密
			String sKeyValue = UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_TOKEN, req.getHeader("openId"));
			sessionKey = sKeyValue;
			JSONObject jsonObj = WechatGetUserInfoUtil.getUserInfo(encryptedData, sessionKey, iv);
			if (null != jsonObj) { // 如果类型是0.那么解析用户信息，保存到redis
				Map<String, Object> infoMap = StringUtils.jasonConvertMap(jsonObj.toJSONString());
				if (null != infoMap) {
					String nickName = (String) infoMap.get("nickName");
					String avatarUrl = (String) infoMap.get("avatarUrl");
					String key = UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, req.getHeader("openId"),
							"nickName=" + nickName + "");
					rMessage.setCodeAndMsg(ResultCodeValue.R_SUCCESS, "解密成功" + key);
					rMessage.setData(avatarUrl);
					return rMessage;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new MyException(ResultCodeValue.R_ERROR, "解密不成功");
		}
		return null;

	}

	@Override
	public ResponseMessage<String> saveUserPhone(String encryptedData, String iv, HttpServletRequest req)
			throws Exception {
		String sessionKey = null;
		String openId = req.getHeader("openId");
		String phone = null;
		ResponseMessage<String> rMessage = new ResponseMessage<String>();
		// 通过Head中的openId，得到SessionKey 用于后续的解密
		String sKeyValue = UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_TOKEN, openId);
		sessionKey = sKeyValue;

		JSONObject jsonObj = WechatGetUserInfoUtil.getUserInfo(encryptedData, sessionKey, iv);

		if (null != jsonObj) { // 如果类型是1.那么解析手机号码,保存到redis
			Map<String, Object> infoMap = StringUtils.jasonConvertMap(jsonObj.toJSONString());
			if (null != infoMap) {
				phone = (String) infoMap.get("phoneNumber");
			}
		}
		String value = UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO, openId);
		value = value + UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, openId, value + "&phone=" + phone);
		rMessage.setData(phone);
		rMessage.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
		return rMessage;
	}

	@Override
	public User getUserByMobile(String mobile)
	{
		User user = userDao.getUserByMobile(mobile);
		if (null == user || null == user.getId() || user.getId() < 0 || StringUtils.isEmpty(user.getOpenId())) {
			return null;
		}
		return user;
	}

	/** 考虑角色切换的情况 ***/
	// @Transactional
	@Override
	public synchronized ResponseMessage<User> saveUserInfo(HttpServletRequest req) throws Exception {
		ResponseMessage<User> rMessage = new ResponseMessage<>();
		/*** 1.得到请求头中的用户信息 ***/
		HeadAuthorization headAuth = UserInfoUtil.getLoginHeadInfo(req);
		int admin = 4; // 默认角色设置为0
		// 适配小程序，过滤未授权分支 redis by mengsg
		String phone = headAuth.getPhone();
		if("".equals(phone)|| phone ==null) {
			rMessage.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
			User defaultUser = new User();
			defaultUser.setAdmin(Constant.VISITOR_ROLE);//访客
			defaultUser.setSynId(null);
			defaultUser.setSynMainId(null);
			defaultUser.setSessionKey(headAuth.getSessionKey());
			defaultUser.setOpenId(headAuth.getOpenId());
			defaultUser.setSource(null);
			rMessage.setData(defaultUser);
			return rMessage;
		}
		/*** 2.获取到数据库中此用户的信息,查询此人是否有登录过 ***/
		Map<String, Object> map = new HashMap<>();
		map.put("openId", headAuth.getOpenId());
		User dbUser = getUserByParams(map);

		/*** 3.获取到戎光平台中的管理员信息 ***/
		String info = LesseeInfoUtil.getTenantInfoByTelAndProId(req, phone);
		User rgUser = LesseeInfoUtil.formartLesseeinfo2(info, phone);

		User tenantUser = null;
		/***
		 * 4.获取face++或访客中的一般租户信息,如果项目ID为春笋，那么就去查春笋访客系统数据，如果不为春笋，则去查face++同步的
		 * Subject表的数据
		 ***/
		// 针对系统,因为有可能出现运营平台随便加了一个公司，而访客系统里面不存在这个公司
		boolean isTrueManager = false;
		boolean isChunsun = PropertyUtil.getProperty("chunsun_project_id").equals(headAuth.getProjectId());
		if (isChunsun) {
			// 访问春笋的接口
			Map<String, Object> chunSunMap = chunSunService.chunsunGetEmployee(phone);
			if (null != chunSunMap && chunSunMap.size() > 0) {
				// 与运营平台数据对比
				String department = (String) chunSunMap.get("companyName");
				String floor = (String) chunSunMap.get("floor");
				String mainAdminInfo = LesseeInfoUtil.getTenantInfoByDeptNameAndProId(req,
						URLEncoder.encode(department, "UTF-8"));
				tenantUser = LesseeInfoUtil.formartLesseeinfo2(mainAdminInfo, null);
				System.out.println("通过公司名称查询运营平台数据：" + tenantUser);
				if (null != tenantUser && tenantUser.getAdmin() == Constant.MAIN_ADMIN_ROLE) {
					tenantUser.setAccountNo((String) chunSunMap.get("tel"));
					tenantUser.setDeptName(department);
					tenantUser.setUserName((String) chunSunMap.get("empName"));
					tenantUser.setOpenId(headAuth.getOpenId());
					tenantUser.setWxPhone((String) chunSunMap.get("tel"));
					tenantUser.setAdmin(Constant.LESSEE_ROLE);
					tenantUser.setFloor(floor);
					if(rgUser != null && !StringUtils.isEmpty(rgUser.getDeptName()) && rgUser.getDeptName().equals(department)){
						isTrueManager = true;
					}
				}
			}
		} else {
			/** 5.如果这个人是租户，那么去戎光中获取主管理信息 **/
			// 访问face++接口
			map.put("phone", phone);
			map.put("projectId", headAuth.getProjectId());
			Subject subject = subjcetDao.getSubjectByParams(map);
			if (null != subject) {
				String department = subject.getDepartment();
				String mainAdminInfo = LesseeInfoUtil.getTenantInfoByDeptNameAndProId(req,
						URLEncoder.encode(department, "UTF-8"));
				tenantUser = LesseeInfoUtil.formartLesseeinfo2(mainAdminInfo, null);
				if (null != tenantUser && tenantUser.getAdmin() == Constant.MAIN_ADMIN_ROLE) {
					tenantUser.setAccountNo(subject.getPhone());
					tenantUser.setDeptName(department);
					tenantUser.setUserName(subject.getRealName());
					tenantUser.setFaceId(subject.getFaceId() + "");
					tenantUser.setOpenId(headAuth.getOpenId());
					tenantUser.setWxPhone(subject.getPhone());
					tenantUser.setAdmin(Constant.LESSEE_ROLE);//普通用户
					if(rgUser != null && !StringUtils.isEmpty(rgUser.getDeptName()) && rgUser.getDeptName().equals(department)){
						isTrueManager = true;
					}
				}
			}
		}

		/********************** 5.获取用户的所有项目接口 **************************/

		// 去数据库中查询访客系统同步来的数据，如果没有，再去运营平台的接口中查询用户数据，再没有的话，定义这个人为访客，
		// 登录过
		if (null != dbUser && null != dbUser.getId() && dbUser.getId() > 0) {
			// 同一个微信号登录获取的openId是一致的，如果换手机号授权登录则更新手机号
			if(!phone.equals(dbUser.getWxPhone())){
				User newUser = new User();
				newUser.setId(dbUser.getId());
				newUser.setAccountNo(phone);
				newUser.setWxPhone(phone);
				userDao.updateByPrimaryKeySelective(newUser);
			}
			map.put("userId", dbUser.getId());

			UserProject userProject = userProjectDao.getUserProjectByParams(map);// 如果数据库有数据，且登陆手机号跟用户账号不同，那么直接给提示
			if (!dbUser.getWxPhone().equals(dbUser.getAccountNo())) {
				// 如果当前登录号码，是修改的新号码，那么直接登录
				if (!phone.equals(dbUser.getAccountNo())) {// 如果请求头的phone和用户表中的account不一致，那么删除redis数据
					RedisUtils util = SpringUtils.getBean(RedisUtils.class);
					util.del(RedisKeyPrefix.USER_INFO, req.getHeader("openId"));
					rMessage.setCodeAndMsg(ResultCodeEnum.ERROR.code, "微信号码与绑定的手机号码不一致，请联系管理员");
					return rMessage;
				}
			}
			// 如果运营管理平台没查到，且数据库的角色为租户，或是访客那么直接返回
			if (rgUser == null) {
				if (tenantUser != null) {
					tenantUser.setId(dbUser.getId());
					// userDao.updateByPrimaryKeySelective(tenantUser);
					// 数据更新进入redis by mengsg
					System.out.println("=======================44444====" + dbUser.getSource());
					updateUserInfoRedis(tenantUser);
					rMessage.setData(tenantUser);

				} else {
					dbUser.setAdmin(Constant.VISITOR_ROLE);
					dbUser.setSynId(null);
					dbUser.setSynMainId(null);
					// 此处更新此条数据为访客
					dbUser = getUserInfo(dbUser, Constant.VISITOR_ROLE, null, null, phone, headAuth.getSessionKey(),
							headAuth.getOpenId());
					dbUser.setSource(null);
					dbUser.setId(Integer.parseInt(map.get("userId").toString()));
					// userDao.updateByPrimaryKeySelective(dbUser);
					// 数据更新进入redis by mengsg
					System.out.println("=======================33333====" + dbUser.getSource());
					updateUserInfoRedis(dbUser);
					rMessage.setData(dbUser);
				}

			} else { //运营管理平台有数据
				dbUser = getUserInfo(rgUser, rgUser.getAdmin(), rgUser.getUserName(), rgUser.getDeptName(),
						phone, headAuth.getSessionKey(), headAuth.getOpenId());
				dbUser.setSource("RG");
				dbUser.setId(Integer.parseInt(map.get("userId").toString()));
				// userDao.updateByPrimaryKeySelective(dbUser);
				// 数据更新进入redis by misTrueManagerengsg
				System.out.println("=======================22222====" + dbUser.getSource());
				// 如果访客系统与运营平台的公司不一致,则为普通用户
                if(tenantUser != null){
                    dbUser.setDeptName(tenantUser.getDeptName());
                    dbUser.setFloor(tenantUser.getFloor());
                }
				if(isTrueManager){
					dbUser.setAdmin(Constant.MAIN_ADMIN_ROLE);
				}else if(tenantUser != null){
					dbUser.setAdmin(Constant.LESSEE_ROLE);
				}else {//访客系统没有数据
					dbUser.setAdmin(Constant.VISITOR_ROLE);
					dbUser.setSynId(null);
					dbUser.setSynMainId(null);
					dbUser.setDeptName(null);
					dbUser.setUserName(null);
					dbUser.setSource(null);
				}
				updateUserInfoRedis(dbUser);
				headAuth.setSyncId(dbUser.getSynId());
				headAuth.setSyncMainId(dbUser.getSynMainId());
				headAuth.setRoleCode(dbUser.getAdmin());
				headAuth.setUserId(map.get("userId").toString());
				// 根据请求头中的内容设置userProject
				if (null != userProject && null != userProject.getId()) {
					userProject = userProjecService.saveUserProject(headAuth, userProject.getId());
				} else {
					userProject = userProjecService.saveUserProject(headAuth, 0);
				}
				rMessage.setData(dbUser);
				// 此处更新数据为戎光的管理员
			}
			dbUser.setUserProject(userProject);
			admin = dbUser.getAdmin();
			rMessage.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);
		} else { // 没有登录过
			User addUser;
			// 如果运营平台也没有数据，那么查看face++或访客有无数据
			if (null == rgUser) {
				// 如果face++或者可以查到此人数据
				if (tenantUser != null) {
					headAuth.setFaceId(tenantUser.getFaceId());
					headAuth.setRoleCode(Constant.LESSEE_ROLE);
					addUser = tenantUser;
				} else {
					addUser = getUserInfo(null, Constant.VISITOR_ROLE, null, null, phone, headAuth.getSessionKey(),
							headAuth.getOpenId());
					addUser.setSource(null);
					headAuth.setRoleCode(Constant.VISITOR_ROLE);
				}
				headAuth.setSyncId(addUser.getSynId());
				headAuth.setSyncMainId(addUser.getSynMainId());
			} else { //运营平台有数据
				addUser = getUserInfo(rgUser, rgUser.getAdmin(), rgUser.getUserName(), rgUser.getDeptName(),
						phone, headAuth.getSessionKey(), headAuth.getOpenId());
				addUser.setSource("RG");
				headAuth.setRoleCode(rgUser.getAdmin());
				headAuth.setSyncId(addUser.getSynId());
				headAuth.setSyncMainId(addUser.getSynMainId());
				// 如果访客系统与运营平台的公司不一致,则为普通用户
                if(tenantUser != null){
                    addUser.setDeptName(tenantUser.getDeptName());
                    addUser.setFloor(tenantUser.getFloor());
                }
				if(isTrueManager){
					addUser.setAdmin(Constant.MAIN_ADMIN_ROLE);
				}else if(tenantUser != null){
					addUser.setAdmin(Constant.LESSEE_ROLE);
				}else {//访客系统没有数据
					addUser.setAdmin(Constant.VISITOR_ROLE);
					addUser.setSynId(null);
					addUser.setSynMainId(null);
					addUser.setDeptName(null);
					addUser.setUserName(null);
					addUser.setSource(null);
				}
			}
			admin = addUser.getAdmin();
			// 仅存储用户ID，OPENID
			userDao.insert(addUser);
			// 剩余数据插入redis by mengsg
			System.out.println("=======================11111====" + addUser.getSource());
			updateUserInfoRedis(addUser);
			headAuth.setUserId(addUser.getId() + "");
			UserProject addUserProject = userProjecService.saveUserProject(headAuth, 0);
			addUser.setUserProject(addUserProject);
			rMessage.setData(addUser);
			rMessage.setCodeAndMsg(ResultCodeEnum.SUCCESS.code, ResultCodeEnum.SUCCESS.msg);

		}
		if (headAuth.getSessionKey() != null) {
			Map<String, Object> redisMap = StringUtils.stringConvertMap(headAuth.getSessionKeyValue());
			if (null != redisMap && redisMap.size() > 0) {
				if (null == redisMap.get("admin")) {
					UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, headAuth.getOpenId(),
							headAuth.getSessionKeyValue() + "&admin=" + admin);
				} else {
					if (headAuth.getRoleUpdate().equals("true")) {
						UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, headAuth.getOpenId(), "nickName="
								+ redisMap.get("nickName") + "&phone=" + redisMap.get("phone") + "&admin=" + admin);
					}
				}
				headAuth.setSessionKeyValue(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO, headAuth.getOpenId()));
				if (null == redisMap.get("projectId")) {
					UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, headAuth.getOpenId(),
							headAuth.getSessionKeyValue() + "&projectId=" + headAuth.getProjectId());
				}
			}
		}
		System.out.println("获取的用户信息：" + rMessage.getData());
		return rMessage;
	}

	/**
	 * 将登陆后用户的部分动态数据从redis中查出 by mengsg
	 * 
	 * @param user
	 * @return
	 */
	public User getUserInfoFromRedis(User user) {
		String redisInfo = UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO, user.getOpenId() + "detail");
		if (StringUtils.isNotBlank(redisInfo)) {
			User userNew = JSONObject.parseObject(redisInfo, User.class);
			user.setDeptName(userNew.getDeptName());
			user.setUserName(userNew.getUserName());
			user.setPassWord(userNew.getPassWord());
			user.setNickName(userNew.getNickName());
			user.setOldAccountNo(userNew.getOldAccountNo());
			user.setSessionKey(userNew.getSessionKey());
			user.setAdmin(userNew.getAdmin());
			user.setSynId(userNew.getSynId());
			user.setSource(userNew.getSource());
			user.setSynMainId(userNew.getSynMainId());
		}

		// user.setAccountNo(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		// user.getOpenId()+"accountNo"));
		/*
		 * user.setDeptName(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"deptName"));
		 * user.setUserName(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"userName"));
		 * user.setPassWord(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"passWord"));
		 * user.setNickName(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"nickName")); //
		 * user.setWxPhone(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"wxPhone"));
		 * user.setOldAccountNo(UserInfoUtil.getRedisInfo(RedisKeyPrefix.
		 * USER_INFO, user.getOpenId()+"oldAccountNo"));
		 * user.setSessionKey(UserInfoUtil.getRedisInfo(RedisKeyPrefix.
		 * USER_INFO, user.getOpenId()+"sessionKey"));
		 * if(StringUtils.isNotBlank(UserInfoUtil.getRedisInfo(RedisKeyPrefix.
		 * USER_INFO, user.getOpenId()+"admin"))){
		 * user.setAdmin(Integer.valueOf(UserInfoUtil.getRedisInfo(
		 * RedisKeyPrefix.USER_INFO, user.getOpenId()+"admin"))); }
		 * user.setSynId(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"synId"));
		 * user.setSource(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"source"));
		 * 
		 * user.setSynMainId(UserInfoUtil.getRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"synMainId"));
		 */
		return user;
	}

	/**
	 * 将登陆后用户的部分动态数据存入redis by mengsg
	 * 
	 * @param user
	 * @return
	 */
	public boolean updateUserInfoRedis(User user) {
		JsonObject json = new JsonObject();
		if (StringUtils.isNotBlank(user.getDeptName())) {
			json.addProperty("deptName", user.getDeptName() + "");
		}
		if (StringUtils.isNotBlank(user.getUserName())) {
			json.addProperty("userName", user.getUserName() + "");
		}
		if (StringUtils.isNotBlank(user.getPassWord())) {
			json.addProperty("passWord", user.getPassWord() + "");
		}
		if (StringUtils.isNotBlank(user.getNickName())) {
			json.addProperty("nickName", user.getNickName() + "");
		}
		if (StringUtils.isNotBlank(user.getOldAccountNo())) {
			json.addProperty("oldAccountNo", user.getOldAccountNo() + "");
		}
		if (StringUtils.isNotBlank(user.getSessionKey())) {
			json.addProperty("sessionKey", user.getSessionKey() + "");
		}
		if (null != user.getAdmin()) {
			json.addProperty("admin", user.getAdmin() + "");
		}
		if (StringUtils.isNotBlank(user.getSynId())) {
			json.addProperty("synId", user.getSynId() + "");
		}
		if (StringUtils.isNotBlank(user.getSource())) {
			json.addProperty("source", user.getSource() + "");
		}
		if (StringUtils.isNotBlank(user.getSynMainId())) {
			json.addProperty("synMainId", user.getSynMainId() + "");
		}
		UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO, user.getOpenId() + "detail", json.toString());

		// UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		// user.getOpenId()+"accountNo",user.getAccountNo());
		/*
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"detail",json.toString());
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"userName",user.getUserName()+"");
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"passWord",user.getPassWord()+"");
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"nickName",user.getNickName()+""); //
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"wxPhone",user.getWxPhone());
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"oldAccountNo",user.getOldAccountNo()+"");
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"sessionKey",user.getSessionKey()+"");
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"admin",user.getAdmin()+"");
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"synId",user.getSynId()+"");
		 * if(StringUtils.isNotBlank(user.getSource())){
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"source",user.getSource()+""); }
		 * UserInfoUtil.setRedisInfo(RedisKeyPrefix.USER_INFO,
		 * user.getOpenId()+"synMainId",user.getSynMainId()+"");
		 */
		return true;
	}

//	public static void main(String[] args) {
//		String info = LesseeInfoUtil.getTenantInfoByTelAndProId(null, "15012750715");
//		System.out.println(info);
//		// 宛宛的 ID
//
//	}

	public User getUserInfo(User u, Integer admin, String userName, String deptName, String accountNo,
			String sessionKey, String openId) {
		// 如果是新增数据
		if (null == u) {
			u = new User();
			// u.setRegisterDate(new Date());
		} else {
			u.setUpdateDate(new Date());
		}
		if (4 == admin) {
			u.setAdmin(4);
			u.setUserName("访客");
			u.setDeptName("暂无");
			u.setSynId(null);
			u.setSynMainId(null);
		} else {
			u.setAdmin(admin);
			u.setUserName(userName);
			u.setDeptName(deptName);
		}

		u.setAccountNo(accountNo);
		u.setWxPhone(accountNo);
		u.setOpenId(openId);
		u.setSessionKey(sessionKey);

		return u;
	}

}
